package com.huawei.it.qccdata.utils;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class HttpClientUtils {
    private static PoolingHttpClientConnectionManager connectionManager;

    static {
        //定义一个连接池的工具类对象
        connectionManager = new PoolingHttpClientConnectionManager();
        //定义连接池属性
        //定义连接池最大的连接数
        connectionManager.setMaxTotal(200);
        //定义主机的最大的并发数
        connectionManager.setDefaultMaxPerRoute(20);
    }

    //获取closeHttpClient
    private static CloseableHttpClient getCloseableHttpClient() {

        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();

        return httpClient;
    }


    //执行请求返回HTML页面
    private static String execute(HttpRequestBase httpRequestBase) {
        httpRequestBase.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36");
        httpRequestBase.setHeader("content-type", "application/json");
        httpRequestBase.setHeader("cookie", "QCCSESSID=11bda0ecf8cd9d18f4ba4e18e0; qcc_did=b6eaba03-fc9b-42a9-b6ed-e91e4c645695; UM_distinctid=194e098a07a368-08f89809495e0f-26001951-384000-194e098a07b5da; tfstk=gFpmBWTfAI5baDLLkUWjOPru_Wl-No61wFeOWOQZaa77krevbO7M7apAknB47RYGP-dxG1Qgjt9JMRQxlOxwJO0KJvHpcn65Q23dP7b049Sg3574Wa5PBGXGAlnycn6_V8m7v4xbS2XfshWw7TWPYGXagiPqqTSAjOyV_-PzzasNQO7V_3yPYMza71Sqq375bO7w7sk11S7pQBJrromkeXTtsKIciw-VunKvUA17G37uQRJkZsbUIa2aQLj22AKt7JcO8B_A9wLre-XM4GYAyekUET5HOhIMox2VHQRWhtp--PCDSZ5eE_cUqeXDS6JVZlyGeObfEgJEXW_JEZLwn_oLK1Byp6W2wbgA6tjH7KT0jR8e2HpfNd03zNOAATSpcx2lLnSN4INzTNXQCgo9U5N1ggslJUGglXOV129mq0VpfsS5Awnoq5N1ggslJ0mu9I1VVM_d.; acw_tc=0a47308617389449112214459e0049dea2a89d0ee1021ca016f3796c4bf0e9; CNZZDATA1254842228=1611796243-1738934887-https%253A%252F%252Fwww.baidu.com%252F%7C1738945237"); // 设置请求头为JSON格式
        httpRequestBase.setHeader("dbbf068d86adba414d72", "621ea41b74004c29e3a3eacda68bc51d4b80f4b79b42d9a0edbb5ea44fd38d0709eb37d1c06d408739669612b29c5127dd459391655dcf778771442668de9a2b");
        /**
         * setConnectionRequestTimeout:设置获取请求的最长时间
         *
         * setConnectTimeout: 设置创建连接的最长时间
         *
         * setSocketTimeout: 设置传输超时的最长时间
         */

        RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000)
                .setSocketTimeout(10 * 1000).build();
        httpRequestBase.setConfig(config);


        CloseableHttpClient httpClient = getCloseableHttpClient();

        String result = null;
        try {
            CloseableHttpResponse response = httpClient.execute(httpRequestBase);
            if (response.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (IOException e) {
            System.out.println("http请求出现错误！！！");
        }

        return result;
    }

    //get请求执行
    public static String doGet(String url) throws IOException {
        HttpGet httpGet = new HttpGet(url);

        String html = execute(httpGet);

        return html;

    }

    //post表单参数请求执行
    public static String doPostForm(String url, Map<String, String> param) throws Exception {
        HttpPost httpPost = new HttpPost(url);

        List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();

        for (String key : param.keySet()) {

            list.add(new BasicNameValuePair(key, param.get(key)));
        }
        HttpEntity entity = new UrlEncodedFormEntity(list);
        httpPost.setEntity(entity);

        return execute(httpPost);
    }

    //post表单参数请求执行
    public static String doPost(String url, String jsonStr) {
        HttpPost httpPost = new HttpPost(url);

        StringEntity entity = new StringEntity(jsonStr, "UTF-8");
        httpPost.setEntity(entity);

        return execute(httpPost);
    }
}
